package simple.zombie;

import com.golden.gamedev.object.*;
import com.golden.gamedev.object.background.*;

// JFC
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;

// GTGE
import com.golden.gamedev.*;
import com.golden.gamedev.object.*;

import java.awt.event.KeyEvent;

public class ZombieGame extends Game {
	
  AnimatedSprite  zombie;
  SpriteGroup  ZOMBIE_GROUP;
  
  Background background;
  
  def UP    = 1;
  def RIGHT = 10;
  def DOWN  = 100;
  def LEFT  = 1000;
  def direction = 0;

  def SW = 1;
  def W  = 2;
  def NW = 3;
  def N  = 4;
  def NE = 5;
  def E  = 6;
  def SE = 7;
  def S  = 8;
  
  def directionList     = [ N, NE, E, SE, S, SW, W, NW ];
  def currentDirection  = S;
  def stanceFrameList   = [ 1, 2, 3, 4 ];
  def walkFrameList     = [ 5, 6, 7, 8, 9, 10, 11, 12 ];
  def attackFrameList   = [ 13, 14, 15, 16, 17, 18, 19, 20 ];
  def hitFrameList      = [ 21, 22, 23, 24 ];
  def deadFrameList     = [ 21, 22, 23, 24, 25, 26, 27, 28 ];
  def headShotFrameList = [ 29, 30, 31, 32, 33, 34, 35, 36 ];
  def currentFrameList  = stanceFrameList;
  def currentFrameIndex = 0;
  
  def stepSize          = 1;
  def zombieMoving 			= false;
  
  Timer zombieTimer;

  public void initResources() {
    background = new ColorBackground(Color.GRAY, 800, 600);
    
    zombie = new AnimatedSprite( getImages( "/images/zombie_topdown.png", 36, 8 ), 200, 200 );
    //zombie.setAnimate( true );
    //zombie.setLoopAnim( true );
    
    ZOMBIE_GROUP = new SpriteGroup( "Zombie Group" );
    ZOMBIE_GROUP.add( zombie );
    
    ZOMBIE_GROUP.setBackground(background);
    
    zombieTimer = new Timer( 100 );
  }

  public void update(long elapsedTime) {
    background.update(elapsedTime);

    getZombieDirection();
    moveZombie();
    
    if ( zombieTimer.action( elapsedTime ) ) {
      zombie.setFrame( ( currentDirection - 1 ) * 36 + currentFrameList[ currentFrameIndex ] - 1 );
      
      currentFrameIndex++;
      if( currentFrameIndex >= currentFrameList.size() - 1 ) {
        currentFrameIndex = 0;
      }
      ZOMBIE_GROUP.update( elapsedTime );
    }
  }

  public void render(Graphics2D g) {
    background.render(g);
    
    ZOMBIE_GROUP.render(g);
  }
  
  public void moveZombie() {
  	zombieMoving = false;
    
    zombieSearchingMouse();
    
    if( bsInput.isKeyDown( KeyEvent.VK_UP ) ) {
      movingZombie( UP );
    	zombieMoving = true;
    } else if( bsInput.isKeyDown( KeyEvent.VK_DOWN ) ) {
      movingZombie( DOWN );
    	zombieMoving = true;
    }
    if( bsInput.isKeyDown( KeyEvent.VK_LEFT ) ) {
      movingZombie( LEFT );
    	zombieMoving = true;
    } else if( bsInput.isKeyDown( KeyEvent.VK_RIGHT ) ) {
      movingZombie( RIGHT );
    	zombieMoving = true;
    }
   	
   	if( !zombieMoving ) {
      if( currentFrameList != stanceFrameList ) {
        currentFrameList = stanceFrameList;
        currentFrameIndex = 0;
      }
 		}
  }
  
  def movingZombie( movingDirection ) {
    if( movingDirection == UP ) {
      zombie.y -= stepSize;
    } else if( movingDirection == DOWN ) {
      zombie.y += stepSize;
    }
    if( movingDirection == LEFT ) {
      zombie.x -= stepSize;
    } else if( movingDirection == RIGHT ) {
      zombie.x += stepSize;
    }
    currentFrameList = walkFrameList;
  }

  public void getZombieDirection() {
    direction = 0;
    
    if( bsInput.isKeyDown( KeyEvent.VK_UP ) ) {
      direction += UP;
    } else if( bsInput.isKeyDown( KeyEvent.VK_DOWN ) ) {
      direction += DOWN;
    }
    if( bsInput.isKeyDown( KeyEvent.VK_LEFT ) ) {
      direction += LEFT;
    } else if( bsInput.isKeyDown( KeyEvent.VK_RIGHT ) ) {
      direction += RIGHT;
    }
    changeDirection();
  }
  
  def changeDirection() {
    switch( direction ) {
      case( 1 )   : currentDirection = N; break;
      case( 10 )  : currentDirection = E; break;
      case( 11 )  : currentDirection = NE; break;
      case( 100 ) : currentDirection = S; break;
      case( 110 ) : currentDirection = SE; break;
      case( 1100 ): currentDirection = SW; break;
      case( 1000 ): currentDirection = W; break;
      case( 1001 ): currentDirection = NW; break;
    }
  }
  
  def zombieSearchingMouse() {
    def catchTheMouse = false;
    if( zombie.x <= bsInput.getMouseX() && zombie.x + zombie.width >= bsInput.getMouseX() &&
        zombie.y <= bsInput.getMouseY() && zombie.y + zombie.height >= bsInput.getMouseY() ) {
      catchTheMouse = true;
    } else {
      if( zombie.x + zombie.width < bsInput.getMouseX() ) {
        direction += RIGHT;
        movingZombie( RIGHT );
      } else if( zombie.x > bsInput.getMouseX() ) {
        direction += LEFT;
        movingZombie( LEFT );
      }
      if( zombie.y + zombie.height < bsInput.getMouseY() ) {
        direction += DOWN;
        movingZombie( DOWN );
      } else if( zombie.y > bsInput.getMouseY() ) {
        direction += UP;
        movingZombie( UP );
      }
      
      changeDirection();
      zombieMoving = true;
    }
  }

  public static void main(String[] args) {
    GameLoader game = new GameLoader();
    game.setup(new ZombieGame(), new Dimension(640,480), false);
    game.start();
  }
}